#include <GL/glut.h>
#include <unistd.h>
#include <math.h>
#include <iostream>
#include <assert.h>
#include "globals_.h"
#include <vector>
#include "figure.h"
#include "animation.h"
#include <algorithm>

#include <boost/random.hpp>
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_01.hpp>

using namespace std;

vector<Figure *> shapes;

void functorAnimate(Figure *ptr){
	animateFigure(ptr);
}

void functorDraw(Figure *ptr){
	ptr->draw();
}

void Animation(int unused){

	for_each(shapes.begin(),shapes.end(),functorAnimate);

	glutPostRedisplay();
	glutTimerFunc(TIME,Animation,0);
}

void Mostrar(){
	glClearColor(1,1,1,1);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

//	gluOrtho2D(0,WIDTH,0-(round((float)radio_)/100.0),HEIGHT+(round((float)radio_)/100.0));
	gluOrtho2D(0,WIDTH,0-(round((float)radio_)),HEIGHT+(round((float)radio_)));

	for_each(shapes.begin(),shapes.end(),functorDraw);

	glFlush();
}


int main(int argc, char** argv){

	Figure *F1,*F2,*F3,*F4,*F5;
	F1 = new Rectangle(pair<int,int>(WIDTH/2,HEIGHT),radio_);
	F2 = new Triangle(pair<int,int>((WIDTH/4),HEIGHT),radio_);
	F3 = new Triangle(pair<int,int>((WIDTH/2)+(WIDTH/4),HEIGHT),radio_);

	F4 = new Rectangle(pair<int,int>((WIDTH/8),HEIGHT),radio_);
	F5 = new Rectangle(pair<int,int>((WIDTH/2)+(WIDTH/4)+(WIDTH/8),HEIGHT),radio_);

	shapes.push_back(F1);
	shapes.push_back(F2);
	shapes.push_back(F3);
	shapes.push_back(F4);
	shapes.push_back(F5);

	glutInit(&argc, argv);

	glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	glutInitWindowPosition(100, 100);

	glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );


	glutCreateWindow("Falling Figure !");
	glutDisplayFunc(Mostrar);
	glutTimerFunc(TIME,Animation,0);

	glutMainLoop();

	return 0;
}
